package com.qing.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.springbootinit.common.ErrorCode;
import com.qing.springbootinit.exception.BusinessException;
import com.qing.springbootinit.mapper.ChatroomMapper;
import com.qing.springbootinit.model.dto.chat.ChatQueryRequest;
import com.qing.springbootinit.model.dto.chat.ChatRoomDto;
import com.qing.springbootinit.model.entity.ChatMessages;
import com.qing.springbootinit.model.entity.ChatRoom;
import com.qing.springbootinit.model.entity.User;
import com.qing.springbootinit.service.ChatmessagesService;
import com.qing.springbootinit.service.ChatroomService;
import com.qing.springbootinit.service.UserService;

import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author <a href="https://github.com/qingfengzy">qingfengzy</a>
* @description 针对表【chatroom(聊天房间表)】的数据库操作Service实现
* @createDate 2025-04-29 20:50:06
*/
@Service
public class ChatroomServiceImpl extends ServiceImpl<ChatroomMapper, ChatRoom>
    implements ChatroomService {

    @Resource
    private UserService userService;

    @Resource
    @Lazy
    private ChatmessagesService chatmessagesService;


    @Override
    public Page<ChatRoomDto> listCharRoomPage(ChatQueryRequest chatQueryRequest, HttpServletRequest request) {
//        首先取得当前用户
        User loginUser =  userService.getLoginUser(request);
//        ThrowUtils.throwIf(StringUtils.isBlank(loginUser));
        if(loginUser == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"登录用户不存在");
        }
//        取得用户之后就写出id
        long userId = loginUser.getId();
//        然后根据请求的userId来获取整个房间的列表
        long pageSize = chatQueryRequest.getPageSize();
        long current = chatQueryRequest.getCurrent();
        Page<ChatRoom> page = new Page<>(current,pageSize);
        Page<ChatRoomDto> PageDto = new Page<>(current,pageSize);

        LambdaQueryWrapper<ChatRoom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatRoom::getStatus,0);
//        根据时间降序来排列 也可以在前端搞
        queryWrapper.orderByDesc(ChatRoom::getCreateTime);
//        然后获取的是当前用户id的数据
        queryWrapper.eq(ChatRoom::getUserId,chatQueryRequest.getUserId());
        page = this.page(page,queryWrapper);


//        然后转化为包装类 dto
        BeanUtils.copyProperties(page,PageDto,"records");

        // 手动处理 records 列表
        List<ChatRoom> records = page.getRecords();
        List<ChatRoomDto> chatRoomDtoList = records.stream().map(chatRoom -> {
            ChatRoomDto chatRoomDto = new ChatRoomDto();
            BeanUtils.copyProperties(chatRoom,chatRoomDto);
            String roomId= String.valueOf(chatRoom.getId());
//            设置某个房间的全部聊天信息
            List<ChatMessages> chatMessageList = getCharMessageById(roomId);
            chatRoomDto.setChatMessageList(chatMessageList);
            return chatRoomDto;
        }).collect(Collectors.toList());

        PageDto.setRecords(chatRoomDtoList);
        return PageDto;
    }

    /**
     * 获取某个房间的全部聊天信息
     * @param roomId
     * @return
     */
    private List<ChatMessages> getCharMessageById(String roomId){
        LambdaQueryWrapper<ChatMessages> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatMessages::getRoomId,roomId);
        List<ChatMessages> chatMessageList = chatmessagesService.list(queryWrapper);
        if(chatMessageList == null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return chatMessageList;
    }


}




